﻿using System;
using System.Collections.Generic;
using System.Text;
using FeCMS.Common.Interface;
using FeCMS.Common;
using FeCMS.CommonFunction;

namespace FeCMS.AccessControlModule.handle
{
    /// <summary>
    ///     操作的基类
    /// </summary>
    /// <typeparam name="E">实体类</typeparam>
    /// <typeparam name="I">ID[int或string]</typeparam>
    internal abstract class BaseHandle<E,I>
    {
        #region 私有变量

        /// <summary>
        ///     私有变量，用来暂时存KV
        /// </summary>
        protected  Dictionary<I, E> _obj;

        #endregion


        #region 事件

        public delegate void DelegateUpdate();
        /// <summary>
        ///     设置更新的委托
        /// </summary>
        public DelegateUpdate SetDelegateUpdate
        {
            get;
            set;
        }

        public delegate int GetIndex(HandleType type);
        /// <summary>
        ///     设置获取主键索引值
        /// </summary>
        public GetIndex SetDelegateGetIndex
        {
            get;
            set;
        }


        #endregion

        /// <summary>
        ///     构造函数
        /// </summary>
        /// <param name="obj"></param>
        public  BaseHandle(Dictionary<I, E> obj)
        {
            this._obj = obj;
        }

        /// <summary>
        ///     添加，并返回相应的主键
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public virtual int Add(E e)
        {
            return -1;
        }

        /// <summary>
        ///     添加
        /// </summary>
        /// <param name="i">主键</param>
        /// <param name="e">实体</param>
        /// <returns></returns>
        public virtual bool Add(I i, E e)
        {
            //TypeCode typecode = Type.GetTypeCode(i.GetType());
            //String类型的主键处理
            if (!_obj.ContainsKey(i))
            {
                _obj.Add(i, e);
                SetDelegateUpdate();
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///     删除
        /// </summary>
        /// <param name="t">主键</param>
        /// <returns></returns>
        public virtual bool Del(I i)
        {
            if (_obj.ContainsKey(i))
            {
                _obj.Remove(i);
                SetDelegateUpdate();
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        ///     修改
        /// </summary>
        /// <param name="i">主键</param>
        /// <param name="e">实体</param>
        /// <returns></returns>
        public virtual bool Edit(I i, E e)
        {
            if (_obj.ContainsKey(i))
            {
                _obj[i] = e;
                SetDelegateUpdate();
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        ///     搜索
        /// </summary>
        /// <typeparam name="V">查询的值类型</typeparam>
        /// <param name="obj">查询的实体</param>
        /// <param name="id">查询的键</param>
        /// <returns></returns>
        public virtual ResponseResult<E, IErrorCode> Search(I id)
        {
            //检测值是否存在，不存在返回错误
            if (_obj.ContainsKey(id))
            {
                return new ResponseResult<E, IErrorCode>(_obj[id]);
            }
            else
            {
                return new ResponseResult<E, IErrorCode>(
                    Function.MergeErrorCode(
                       ErrorCode.ErrorCodeTemplate_Search,
                       ErrorCode.ErrorCodeReason_DoesNotFound
                    )
                );
            }
        }


    }

    

}
